package org.test.once.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.test.once.dao.UserDao;
import org.test.once.model.User;
import org.test.once.utils.DBHelper;

public class UserDao {
	
	private static UserDao instance;

	private UserDao() {
	}

	/**
	 * 获取此类的一个实例
	 * 
	 * @return
	 */
	public static UserDao getInstance() {
		if (instance == null) {
			instance = new UserDao();
		}
		return instance;
	}

	/**
	 * 添加用户
	 * 
	 * @param user
	 * @throws SQLException
	 */
	public void save(User user) throws SQLException {
		String sql = "insert into t_user(name, account, password, role_id) values(?, ?, ?, ?)";

		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, user.getUserName());
		pstmt.setString(2, user.getAccount());
		pstmt.setString(3, user.getPassword());
		pstmt.setString(4, null);
		pstmt.execute();

		DBHelper.close(pstmt);
		DBHelper.close(conn);
	}

	/**
	 * 通过Id查询
	 * 
	 * @param id
	 * @throws SQLException
	 */
	public User findUserById(int id) throws SQLException {
		String sql = "select * from t_user where id = ?";

		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, id);
		ResultSet rs = pstmt.executeQuery();
		User user = null;
		if (rs.next()) {
			user = new User();
			user.setId(id);
			user.setUserName(rs.getString("name"));
			user.setAccount(rs.getString("account"));
			user.setPassword(rs.getString("password"));
		}

		DBHelper.close(rs);
		DBHelper.close(pstmt);
		DBHelper.close(conn);
		return user;
	}

	/**
	 * 
	 * @param account
	 * @return
	 * @throws SQLException
	 */
	public User findUserByAccount(String account) throws SQLException {
		String sql = "select * from t_user where account = ?";

		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, account);
		ResultSet rs = pstmt.executeQuery();
		User user = null;
		if (rs.next()) {
			user = new User();
			user.setId(rs.getInt("id"));
			user.setUserName(rs.getString("name"));
			user.setAccount(rs.getString("account"));
			user.setPassword(rs.getString("password"));
			user.setRoleId(rs.getInt("role_id"));
		}

		DBHelper.close(rs);
		DBHelper.close(pstmt);
		DBHelper.close(conn);
		return user;
	}

	/**
	 * 查询用户列表
	 * 
	 * @return
	 * @throws SQLException
	 */
	public List<User> searchUsers() throws SQLException {
		String sql = "select * from t_user";
		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		ArrayList<User> users = new ArrayList<User>();
		while (rs.next()) {
			User u = new User();
			u.setId(rs.getInt("id"));
			u.setUserName(rs.getString("name"));
			u.setAccount(rs.getString("account"));
			u.setPassword(rs.getString("password"));
			users.add(u);
		}
		DBHelper.close(rs);
		DBHelper.close(pstmt);
		DBHelper.close(conn);
		return users;
	}

	/**
	 * 通过id删除用户
	 * 
	 * @param id
	 * @throws SQLException
	 */
	public void deleteUser(int id) throws SQLException {
		String sql = "delete from t_user where id = ?";

		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, id);
		pstmt.execute();

		DBHelper.close(pstmt);
		DBHelper.close(conn);
	}

	/**
	 * 更新用户信息
	 * 
	 * @param user
	 * @throws SQLException 
	 */
	public void updateUser(User user) throws SQLException {
		String sql = "update t_user set name = ?, account = ?, password = ? where id = ?";

		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, user.getUserName());
		pstmt.setString(2, user.getAccount());
		pstmt.setString(3, user.getPassword());
		pstmt.setInt(4, user.getId());
		pstmt.executeUpdate();

		DBHelper.close(pstmt);
		DBHelper.close(conn);
	}
	
	public void updateUserRole(int role_id, int id) {
		String sql = "update t_user set role_id=? where id=?";

		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, role_id);
			pstmt.setInt(2, id);
			pstmt.executeUpdate();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}finally{
			DBHelper.close(pstmt);
			DBHelper.close(conn);
		}
	}
}
